#include "Energy.h"

module EnergyAppP {
	
	uses {
		interface Boot;
		interface EnergyState;
		interface EnergyFlags;
		interface Timer<TMilli>;
		interface Leds;
	}

} implementation{	

	enum {
		NUMBER_STATES = 4,
	};

	energy_state_t state;
	bool toggle;
	
	task void stateTask() {
		state = (energy_state_t) ((state + 1) % NUMBER_STATES);
		//state = ENERGY_STATE4;
		if(call EnergyState.setState(state) != SUCCESS) {
			call Leds.led0On();
			call Leds.led1On();
			call Leds.led2On();
			call Timer.stop();
			return;
		}
//		call Leds.set(state);
	}

	task void flagTask() {
		if(toggle) {
			call EnergyFlags.setFlag(ENERGY_FLAG1);
			call EnergyFlags.setFlag(ENERGY_FLAG2);
			call EnergyFlags.setFlag(ENERGY_FLAG3);
			call Leds.led0On();
			toggle = FALSE;
		} else {
			call EnergyFlags.clearFlag(ENERGY_FLAG1);
			call EnergyFlags.clearFlag(ENERGY_FLAG2);
			call EnergyFlags.clearFlag(ENERGY_FLAG3);
			call Leds.led0Off();
			toggle = TRUE;
		}
	}

	event void Boot.booted() {
		state = ENERGY_STATE1;
		toggle = FALSE;
		call Timer.startPeriodic(1024);		
	}		
	
	event void Timer.fired() {
		//post stateTask();
		post flagTask();
	}
	
}
